package org.sharding.config;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MyRangeTableShardingAlgorithm implements RangeShardingAlgorithm<Long> {
    /**
     * @param availableTargetNames 表示可选的数据源值 - 这里对应的是 数据表名 集合
     * @param rangeShardingValue   分片键对应的值
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> rangeShardingValue) {
        List<String> stringList = new ArrayList<>();

        // select * from course where cid between 100 and 200
        rangeShardingValue.getValueRange().upperEndpoint(); // 范围查询中最大的值
        rangeShardingValue.getValueRange().lowerEndpoint(); // 范围查询中最小的值

        // 逻辑表的名称
        String logicTableName = rangeShardingValue.getLogicTableName();
        // 拼接成真实表
        stringList.add(logicTableName + "_1");
        stringList.add(logicTableName + "_2");
        return stringList;
    }
}
